﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FzWeb.Models
{
    public class BaseDataService
    {

        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"] = new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }


        #region education
        public IList<Education> GetEducationList()
        {
            return db.Education.OrderBy(c => c.Order).ToList();
        }

        public void CreateEducation(Education e)
        {
            db.AddToEducation(e);
            db.SaveChanges();
        }

        public Education GetEducation(Func<Education, bool> predicate)
        {
            return db.Education.First(predicate);
        }

        public void UpdateEducation(Education e)
        {
            var originalEducation = db.Education.Where(c => c.Id == e.Id).FirstOrDefault();
            db.ApplyPropertyChanges(originalEducation.EntityKey.EntitySetName, e);
            db.SaveChanges();
        }

        public Education GetBeforeEducation(int order)
        {
            return db.Education.Where(c => c.Order <= order - 1).OrderByDescending(c => c.Order).First();
        }

        public Education GetAfterEducation(int order)
        {
            return db.Education.Where(c => c.Order >= order + 1).OrderBy(c => c.Order).First();
        }

        public void DeleteEducation(Education e)
        {
            db.DeleteObject(e);
            db.SaveChanges();
        }
        #endregion

        #region WorkExperience
        public IList<WorkExperience> GetWorkExperienceList()
        {
            return db.WorkExperience.OrderBy(c => c.Order).ToList();
        }

        public void CreateWorkExperience(WorkExperience we)
        {
            db.AddToWorkExperience(we);
            db.SaveChanges();
        }

        public WorkExperience GetWorkExperience(Func<WorkExperience, bool> predicate)
        {
            return db.WorkExperience.First(predicate);
        }

        public void UpdateWorkExperience(WorkExperience we)
        {
            db.ApplyPropertyChanges(GetWorkExperience(c => c.Id == we.Id).EntityKey.EntitySetName, we);
            db.SaveChanges();
        }

        public void DeleteWorkExperience(WorkExperience we)
        {
            db.DeleteObject(we);
            db.SaveChanges();
        }

        internal WorkExperience GetAfterWorkExperience(int p)
        {
            return db.WorkExperience.Where(c => c.Order >= p + 1).OrderBy(c => c.Order).First();
        }

        internal WorkExperience GetBeforeWorkExperience(int p)
        {
            return db.WorkExperience.Where(c => c.Order <= p - 1).OrderByDescending(c => c.Order).First();
        }
        #endregion

        #region Area
        public IList<Area> GetAreaList()
        {
            return db.Area.OrderBy(a => a.Order).ToList();

        }

        public IList<Area> GetTopAreaList()
        {
            return db.Area.Where(a => (a.Parent == null)).OrderBy(a => a.Order).ToList();
        }

        public IList<Area> GetChildsAreaList(int areaid)
        {
            return db.Area.Where(c => (c.Parent.AreaId == areaid)).OrderBy(c => c.Order).ToList();
        }

        public IList<Area> GetChildsAreaList(Func<Area, bool> predict)
        {
            return db.Area.Where(predict).OrderBy(c => c.Order).ToList();
        }

        public void CreateArea(Area a)
        {
            db.AddToArea(a);
            db.SaveChanges();
        }

        public Area GetArea(Func<Area, bool> predicate)
        {
            return db.Area.First(predicate);
        }

        public void UpdateArea(Area a)
        {
            var oArea = db.Area.Where(d => d.AreaId == a.AreaId).FirstOrDefault();
            db.ApplyPropertyChanges(oArea.EntityKey.EntitySetName, a);
            db.SaveChanges();
        }

        public void DeleteArea(Area a)
        {
            a.Childs.Load();
            if (a.Childs.Count == 0)
                db.DeleteObject(a);
            db.SaveChanges();
        }


        //无分类 第一层调这个
        public Area GetBeforeArea(int order)
        {
            return db.Area.Where(c => ((c.Order <= order - 1) && (c.Parent == null))).OrderByDescending(c => c.Order).First();
        }

        //有PARENT 调这个
        public Area GetBeforeArea(int order, Area parent)
        {
            return db.Area.Where(c => ((c.Order <= order - 1) && (c.Parent.AreaId == parent.AreaId))).OrderByDescending(c => c.Order).First();
        }

        public Area GetAfterArea(int order)
        {
            return db.Area.Where(c => ((c.Order >= order + 1) && (c.Parent == null))).OrderBy(c => c.Order).First();
        }

        public Area GetAfterArea(int order, Area parent)
        {
            return db.Area.Where(c => ((c.Order >= order + 1) && (c.Parent.AreaId == parent.AreaId))).OrderBy(c => c.Order).First();
        }

        #endregion

        #region PoliticalStatus
        public IList<PoliticalStatus> GetPoliticalStatusList()
        {
            return db.PoliticalStatus.OrderBy(c => c.Order).ToList();
        }
        //
        public void CreatePoliticalStatus(PoliticalStatus p)
        {
            db.AddToPoliticalStatus(p);
            db.SaveChanges();
        }

        public PoliticalStatus GetPoliticalStatus(Func<PoliticalStatus, bool> predicate)
        {
            return db.PoliticalStatus.First(predicate);
        }

        public void UpdatePoliticalStatus(PoliticalStatus p)
        {
            db.ApplyPropertyChanges(GetPoliticalStatus(c => c.Id == p.Id).EntityKey.EntitySetName, p);
            db.SaveChanges();
        }

        public void DeletePoliticalStatus(PoliticalStatus p)
        {
            db.DeleteObject(p);
            db.SaveChanges();
        }
        internal PoliticalStatus GetAfterPoliticalStatus(int p)
        {
            return db.PoliticalStatus.Where(c => c.Order >= p + 1).OrderBy(c => c.Order).First();
        }

        internal PoliticalStatus GetBeforePoliticalStatus(int p)
        {
            return db.PoliticalStatus.Where(c => c.Order <= p - 1).OrderByDescending(c => c.Order).First();
        }
        #endregion

        #region CompanyType
        public IList<CompanyType> GetCompanyTypeList()
        {
            return db.CompanyType.OrderBy(c => c.Order).ToList();
        }

        public IList<Company> GetCompanyList()
        {
            return db.Company.OrderBy(c => c.Order).ToList();
        }

        public void CreateCompanyType(CompanyType c)
        {
            db.AddToCompanyType(c);
            db.SaveChanges();
        }

        public CompanyType GetCompanyType(Func<CompanyType, bool> predicate)
        {
            return db.CompanyType.First(predicate);
        }

        public void UpdateCompanyType(CompanyType c)
        {
            db.ApplyPropertyChanges(GetCompanyType(t => t.Id == c.Id).EntityKey.EntitySetName, c);
            db.SaveChanges();
        }

        public void DeleteCompanyType(CompanyType c)
        {
            db.DeleteObject(c);
            db.SaveChanges();
        }
        internal CompanyType GetAfterCompanyType(int p)
        {

            return db.CompanyType.Where(c => c.Order >= p + 1).OrderBy(c => c.Order).First();
        }

        internal CompanyType GetBeforeCompanyType(int p)
        {
            return db.CompanyType.Where(c => c.Order <= p - 1).OrderByDescending(c => c.Order).First();
        }
        #endregion



        #region Post_Class
        public IList<Post_Class> GetPost_ClassList()
        {
            return db.Post_Class.OrderBy(c => c.Order).ToList();
        }

        public IList<Post_Class> GetTopPost_ClassList()
        {
            return db.Post_Class.Include("Post_Childs").Include("Post_Parent").Where(c => (c.Post_Parent == null)).OrderBy(c => c.Order).ToList();
        }

        public void CreatePost_Class(Post_Class pc)
        {
            db.AddToPost_Class(pc);
            db.SaveChanges();
        }

        public Post_Class GetPost_Class(Func<Post_Class, bool> predicate)
        {
            return db.Post_Class.First(predicate);
        }

        public void UpdatePost_Class(Post_Class pc)
        {
            var oPost_Class = db.Post_Class.Where(d => d.PostClassId == pc.PostClassId).FirstOrDefault();
            db.ApplyPropertyChanges(oPost_Class.EntityKey.EntitySetName, pc);
            db.SaveChanges();
        }

        public void DeletePost_Class(Post_Class pc)
        {
            //只有子类为空才能做父类删除
            pc.Post_Childs.Load();
            if (pc.Post_Childs.Count == 0)
            {
                db.DeleteObject(pc);
                db.SaveChanges();
            }
        }

        //有主从关系的用第2个方法 可以调出同级对象 而不是全体对象
        public Post_Class GetBeforePost_Class(int order)
        {
            return db.Post_Class.Where(c => ((c.Order <= order - 1) && (c.Post_Parent == null))).OrderByDescending(c => c.Order).First();
        }

        public Post_Class GetAfterPost_Class(int order)
        {
            return db.Post_Class.Where(c => ((c.Order >= order + 1) && (c.Post_Parent == null))).OrderBy(c => c.Order).First();
        }

        public Post_Class GetBeforePost_Class(int order, Post_Class parent)
        {
            return db.Post_Class.Where(c => ((c.Order <= order - 1) && (c.Post_Parent.PostClassId == parent.PostClassId))).OrderByDescending(c => c.Order).First();
        }

        public Post_Class GetAfterPost_Class(int order, Post_Class parent)
        {
            return db.Post_Class.Where(c => ((c.Order >= order + 1) && (c.Post_Parent.PostClassId == parent.PostClassId))).OrderBy(c => c.Order).First();
        }



        #endregion

        #region Site_Comfig
        public Site_Config GetSiteConfig()
        {
            return db.Site_Config.First();
        }
        public void UpdateSiteConfig(Site_Config s)
        {
            db.ApplyPropertyChanges(GetSiteConfig().EntityKey.EntitySetName, s);

            db.SaveChanges();
        }
        #endregion

        #region News
        public IList<News> GetNewsList()
        {
            return db.News.Include("news_class").OrderByDescending(c => c.AddTime).ToList();
        }

        public IList<News> GetNewsList(int classId)
        {
            var id = GetNewsClass(classId).Id;
            return db.News.Include("news_class").Where(c => c.News_Class.Id == id).OrderBy(c => c.Order).ToList();
        }

        public IList<News> GetNewsList(Func<News, bool> predict)
        {
            return db.News.Include("news_class").Where(predict).OrderByDescending(c => c.AddTime).ToList();
        }

        public IList<News> GetNewsList(Func<News, bool> predict, int count)
        {
            return db.News.Include("news_class").Where(predict).OrderByDescending(c => c.AddTime).Take(count).ToList();
        }

        public News GetNews(int id)
        {
            return db.News.Include("news_class").First(c => c.NewsId == id);
        }

        public void UpdateNews(News n)
        {
            db.ApplyPropertyChanges(GetNews(n.NewsId).EntityKey.EntitySetName, n);
            db.SaveChanges();
        }
        public void DeleteNews(News n)
        {
            db.DeleteObject(n);
            db.SaveChanges();
        }
        public void CreateNews(News news)
        {
            db.AddToNews(news);
            db.SaveChanges();
        }

        public IList<News_Class> GetNewsClassList()
        {
            return db.News_Class.Include("news_childs").Include("news_parent").Where(c => c.News_Parent == null).ToList();
        }

       

        public News_Class GetNewsClassAll(int id)
        {
            return db.News_Class.Include("news_childs").Include("news_parent").Include("news_childs.news").First(c => c.Id == id);
        }

        public News_Class GetNewsClass(int id)
        {
            return db.News_Class.Include("news_childs").Include("news_parent").First(c => c.Id == id);
        }

        internal News GetLastNews(int id)
        {
            var newsClass = GetNews(id).News_Class;
            if (db.News.Include("news_class").Where(c => c.NewsId < id && c.News_Class.Id == newsClass.Id).Count() < 1)
                return new News();
            return db.News.Where(c => c.NewsId < id && c.News_Class.Id == newsClass.Id).OrderByDescending(c => c.NewsId).First();
        }

        internal News GetAfterNews(int id)
        {
            var newsClass = GetNews(id).News_Class;
            if (db.News.Where(c => c.NewsId > id && c.News_Class.Id == newsClass.Id).Count() < 1)
                return new News();
            return db.News.Where(c => c.NewsId > id && c.News_Class.Id == newsClass.Id).First();
        }

        //0-hot 1-new
        public IList<News> GetTop10(int id)
        {
            if (id == 1) return db.News.OrderByDescending(c => c.AddTime).Take(10).ToList();
            else
                return db.News.OrderByDescending(c => c.ViewNo).Take(10).ToList();
        }

        public IList<News> GetTop20(int id)
        {
            if (id == 1) return db.News.OrderByDescending(c => c.AddTime).Take(20).ToList();
            else
                return db.News.OrderByDescending(c => c.ViewNo).Take(20).ToList();
        }

        #endregion

        #region 广告
        public IList<Ad> GetAdList()
        {
            return db.Ad.ToList();
        }

        public Ad GetAd(int id)
        {
            return db.Ad.First(c => c.AdId == id);
        }

        internal void CreateAd(Ad ad)
        {
            db.AddToAd(ad);
            db.SaveChanges();
        }

        public void UpdateAd(Ad ad)
        {
            db.ApplyPropertyChanges(GetAd(ad.AdId).EntityKey.EntitySetName, ad);
            db.SaveChanges();
        }

        internal void DeleteAd(int id)
        {
            db.DeleteObject(GetAd(id));
            db.SaveChanges();
        }
        #endregion


        public Vocation GetVocation(string name)
        {
            return db.Vocation.First(c => c.Name == name);
        }

        #region
        internal Post_Param getPostParam(int id)
        {
            return db.Post_Param.First(c => c.PostParamId == id);
        }

        public IList<Post_Param> getPostParams(int id)
        {
            return db.Post_Param.Where(c => c.PostParamId == id).ToList();
        }
        #endregion

    }
}
